package com.ksd.web.controller.tool;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class ChinesePinyinConverter {


    // 定义特殊字符的正则表达式
    private static final String SPECIAL_CHAR_REGEX = "[^a-zA-Z0-9\\u4e00-\\u9fa5]";

    // 去除字符串中的特殊字符
    public static String removeSpecialCharacters(String input) {
        return input.replaceAll(SPECIAL_CHAR_REGEX, "");
    }

    // 判断字符串是否为中文
    public static boolean isChinese(String input) {
        for (char c : input.toCharArray()) {
            if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
                return true;
            }
        }
        return false;
    }

    // 将中文字符串转换为拼音
    public static String chineseToPinyin(String input) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        StringBuilder pinyin = new StringBuilder();
        for (char c : input.toCharArray()) {
            try {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if (pinyinArray != null) {
                    System.out.println("pinyinArray:"+pinyinArray[0]);
                    pinyin.append(pinyinArray[0].substring(0,1)).append("");
                    System.out.println("pinyin:"+pinyin.toString());
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        }
        return pinyin.toString().trim();
    }

    public static String processString(String input) {
        // 去除特殊字符
        String cleanInput = removeSpecialCharacters(input);
        System.out.println("cleanInput:" + cleanInput);

        if (isChinese(cleanInput)) {
            // 如果是中文，转换为拼音
            return chineseToPinyin(cleanInput);
        } else {
            // 如果是英文，直接返回
            return cleanInput;
        }
    }

    public static void main(String[] args) {
        String testString1 = "Hello!@# World";
        String testString2 = "你好，世界！";
        String testString3 = "你好,DemoHelloWordLove";

//        System.out.println(processString(testString1));
//        System.out.println(processString(testString2));
//        System.out.println(processString(testString3));
        boolean chinese = isChinese(testString1);
        System.out.println(chinese);

        String trim = "Hello 世界，Java 编程！  ";
//        String trim = StringUtils.trim(input);
        System.out.println(trim);
        String output = convertToPinyin(trim).replaceAll("\\s+","");
        System.out.println(output);
    }

    public static String convertToPinyin(String input) {
        StringBuilder result = new StringBuilder();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        // 设置拼音为小写
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 不显示声调
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            // 判断字符是否为中文字符
            if (Character.toString(c).matches("[\\u4e00-\\u9fa5]")) {
                try {
                    // 获取该中文字符的拼音数组
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null) {
                        // 通常一个中文字符对应一个拼音，取数组的第一个元素
                        result.append(pinyinArray[0].substring(0,1));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                // 非中文字符（如英文字符）直接添加到结果中
                result.append(c);
            }
        }
        return result.toString();
    }
}